package com.example.testapp.activity.activity.study;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Xml;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.activity.SuperActivity;
import com.example.testapp.activity.adapter.BolgCommentAdapter;
import com.example.testapp.activity.http.AsyncHttpUtil;
import com.example.testapp.activity.model.BolgComment;
import com.example.testapp.activity.view.ErrorHintView;
import com.example.testapp.activity.view.listview.PullToRefreshAdapterViewBase;
import com.example.testapp.activity.view.listview.PullToRefreshBase;
import com.example.testapp.activity.view.listview.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by l on 2016/9/19.
 */
public class NewsCommentActivity extends SuperActivity {

    private int pageNext = 1;  //下一页页码
    private boolean flag = false;
    private String  id;
    private LinearLayout footerView;    //最后一条listview
    private PullToRefreshListView  mListView;
    private ErrorHintView   mErrorHintView;
    private TextView        mTitle;

    private static final String STR_BEFORE = "http://wcf.open.cnblogs.com/news/item/";
    private static final String STR_AFTER = "/comments/";

    private List<BolgComment> bolgList = new ArrayList<BolgComment>();
    private List<BolgComment> list = new ArrayList<BolgComment>();
    private BolgCommentAdapter  mAdapter;

    public static int VIEW_LIST = 1;
    /**显示断网**/
    public static int VIEW_WIFIFAILUER = 2;
    /** 显示加载数据失败 **/
    public static int VIEW_LOADFAILURE = 3;
    public static int VIEW_LOADING = 4;
    public static int VIEW_NODATA = 5;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_comment);
        getWindow().setBackgroundDrawable(null);
        initView();
    }

    private void initView() {
        findViewById(R.id.back_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
       mTitle = (TextView)findViewById(R.id.head_title);
       mListView = (PullToRefreshListView)findViewById(R.id.list);
       mErrorHintView = (ErrorHintView)findViewById(R.id.hintView);

        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        String title = intent.getStringExtra("title");
        mTitle.setText(title);

        setUpPullToList();
        showLoading(VIEW_LOADING);
        loadBolgCommentInfo(true);
    }

    /**
     * 设置listview的下拉刷新
     */
    private void setUpPullToList() {
         mAdapter = new BolgCommentAdapter(mContext, bolgList);
         mListView.setAdapter(mAdapter);

        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                 if(refreshView.isHeaderShown()){
                     pageNext = 1;
                     flag = false;
                     loadBolgCommentInfo(true);
                 }
            }
        });

        mListView.setOnPreRefreshingAnimListener(new PullToRefreshAdapterViewBase.OnPreRefreshingAnimListener() {
            @Override
            public void onPreRefreshingAnim() {
                  mListView.setFooterLoadingViewHeaderText("加载更多评论");
            }
        });

        mListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if(pageNext == 0){
                    //没有下一页了
                    mListView.onRefreshComplete();
                    if(footerView == null){
                        footerView = (LinearLayout)View.inflate(mContext, R.layout.item_pull_to_refresh_footer, null);
                        TextView footer = (TextView)footerView.findViewById(R.id.footer);
                        footer.setText("到底了呢-共" + bolgList.size() + "条评论");
                        mListView.getRefreshableView().addFooterView(footerView);
                    }
                }else{
                    //滑动到底部自动刷新
                    flag = false;
                    loadBolgCommentInfo(false);
                }
            }
        });
    }

    private void showLoading(int i) {
        mErrorHintView.setVisibility(View.GONE);
        mListView.setVisibility(View.GONE);

        switch (i){
            case 1:
                mErrorHintView.hideLoading();
                mListView.setVisibility(View.VISIBLE);
                break;
            case 2:
                mErrorHintView.hideLoading();
                mErrorHintView.netError(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        pageNext = 1;
                        flag = false;
                        showLoading(VIEW_LOADING);
                        loadBolgCommentInfo(true);
                    }
                });
                break;
            case 3:
                mErrorHintView.hideLoading();
                mErrorHintView.loadFailure(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        pageNext = 1;
                        flag = false;
                        showLoading(VIEW_LOADING);
                        loadBolgCommentInfo(true);
                    }
                });
                break;
            case 4:
                mErrorHintView.loadingdata();
                break;
            case 5:
                mErrorHintView.hideLoading();
                mErrorHintView.noData();
                break;
        }
    }

    /**
     * 加载评论内容
     * @param clean
     */
    private void loadBolgCommentInfo(final boolean clean) {
        String url = STR_BEFORE + id + STR_AFTER + pageNext +"/20";

        AsyncHttpUtil.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                 if(footerView != null){
                     mListView.getRefreshableView().removeFooterView(footerView);
                     footerView = null;
                 }

                mListView.onRefreshComplete();

                if(responseBody != null && responseBody.length > 0){
                    try {
                        if (clean) {
                            bolgList.size();
                        }
                        list.clear();
                        ByteArrayInputStream bis = new ByteArrayInputStream(responseBody);
                        parseCommentXml(bis);
                        if(list.size() == 0 && list.size() < 20){
                            pageNext = 0;
                        }else{
                            pageNext ++;
                        }
                        bolgList.addAll(list);
                        if(bolgList.size()==0){
                            showLoading(VIEW_NODATA);
                        }else{
                            showLoading(VIEW_LIST);
                            mAdapter.notifyDataSetChanged();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        showLoading(VIEW_LOADFAILURE);
                    }
                }else{
                    showLoading(VIEW_LOADFAILURE);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                  showLoading(VIEW_LOADFAILURE);
            }
        });
    }

    /**
     * 解析评论内容
     *
     * @param bis
     * @throws XmlPullParserException
     * @throws IOException
     */
    private void parseCommentXml(ByteArrayInputStream bis) throws XmlPullParserException, IOException {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(bis, "UTF-8");
        int event = parser.getEventType();
        BolgComment item = null;

        while (event != XmlPullParser.END_DOCUMENT){
            switch (event){
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    if("entry".equals(parser.getName())){
                        flag = true;
                        item = new BolgComment();
                    }

                    if(flag){
                        if("id".equals(parser.getName())){
                            String id = parser.nextText();
                            item.setId(id);
                        }else if("title".equals(parser.getName())){
                            String title = parser.nextText();
                            item.setTitle(title);
                        }else if("published".equals(parser.getName())){
                            String published = parser.nextText();
                            item.setPublished(published);
                        }else if("updated".equals(parser.getName())){
                            String updated = parser.nextText();
                            item.setUpdated(updated);
                        }else if("author".equals(parser.getName())){

                        }else if("name".equals(parser.getName())){
                            String name = parser.nextText();
                            item.setName(name);
                        }else if("uri".equals(parser.getName())){
                            String uri = parser.nextText();
                            item.setUri(uri);
                        }else if("content".equals(parser.getName())){
                            String content = parser.nextText();
                            item.setContent(content);
                        }
                    }
                    break;
                case XmlPullParser.TEXT:
                    break;
                case XmlPullParser.END_TAG:
                    if("entry".equals(parser.getName())){
                        list.add(item);
                    }
                    break;
            }
            event = parser.next();
        }
    }

    @Override
    protected void obtainInfo() {

    }
}
